cmake_minimum_required(VERSION 3.3)

##############CMake features backport#################
# CMake 3.8+ supports an empty list element as referring to the
# configuration-less imported location specified by `IMPORTED_LOCATION`.
if(NOT CMAKE_VERSION VERSION_LESS 3.8.0)
	list(APPEND CMAKE_MAP_IMPORTED_CONFIG_RELEASE Release RelWithDebInfo "")
	list(APPEND CMAKE_MAP_IMPORTED_CONFIG_RELWITHDEBINFO RelWithDebInfo Release "")
endif()


if(CMAKE_VERSION VERSION_LESS 3.12.0)
	function(add_compile_definitions)
		foreach(d ${ARGV})
			list(APPEND ds -D${d})
		endforeach()
		add_definitions(${ds})
	endfunction()

	function(target_link_libraries target)
		foreach(t ${ARGN})
			if(TARGET ${t})
				get_target_property(type ${t} TYPE)
				if(type STREQUAL OBJECT_LIBRARY)
					set(t $<TARGET_OBJECTS:${t}>)
				endif()
			endif()
			list(APPEND libs ${t})
		endforeach()
		_target_link_libraries(${target} ${libs})
	endfunction()
endif()
#######################################################

#################Some useful functions###################
function(setup_library target)
	get_target_property(name ${target} NAME)
	get_target_property(type ${target} TYPE)
	string(MAKE_C_IDENTIFIER ${name}_${type} name)
	string(TOUPPER ${name} name)
	target_compile_definitions(${target} PUBLIC ${name})
endfunction()

function(create_whole_library lib)
	string(REPLACE : _ libname "${lib}")
	add_library(${libname}_whole_interface INTERFACE)
	if(MSVC)
		target_link_libraries(${libname}_whole_interface INTERFACE -WHOLEARCHIVE:$<TARGET_LINKER_FILE:${lib}> ${lib})
	elseif(CMAKE_C_COMPILER_ID STREQUAL AppleClang)
		target_link_libraries(${libname}_whole_interface INTERFACE -Wl,-force_load,$<TARGET_LINKER_FILE:${lib}> ${lib})
	else()
		target_link_libraries(${libname}_whole_interface INTERFACE -Wl,--whole-archive ${lib} -Wl,--no-whole-archive)
	endif()
	add_library(${lib}_whole ALIAS ${libname}_whole_interface)
endfunction()
#########################################################

##########Set some global cmake variables##############
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_VERBOSE_MAKEFILE ON CACHE BOOL "")
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
#########################################################

#######################Unix#################################
# Make PIC even for static libraries such that they can be linked into shared libraries.
# Hidden all symbols by default, that could produce smaller library files.
# Compatible with windows by these settings.
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
set(CMAKE_C_VISIBILITY_PRESET hidden)
set(CMAKE_CXX_VISIBILITY_PRESET hidden)
set(CMAKE_VISIBILITY_INLINES_HIDDEN ON)
#######################Unix#################################

########################Windows################################
# Put .exe and .dll into the same folder.
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
########################Windows################################




project(ExportTest)

include_directories(${CMAKE_CURRENT_SOURCE_DIR})

add_subdirectory(add)
add_subdirectory(sub)
add_subdirectory(op)

add_executable(${PROJECT_NAME} main.cpp)

target_link_libraries(${PROJECT_NAME} PUBLIC test.op)